﻿using System;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Migrations;

namespace JuCheap.Core.Data.Migrations
{
    public partial class refactor_group : Migration
    {
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropForeignKey(
                name: "FK_PaiBans_BanCis_BanCiId",
                table: "PaiBans");

            migrationBuilder.DropColumn(
                name: "Devices",
                table: "DepartmentGroups");

            migrationBuilder.DropColumn(
                name: "Workers",
                table: "DepartmentGroups");

            migrationBuilder.AlterColumn<int>(
                name: "BanCiId",
                table: "PaiBans",
                nullable: false,
                oldClrType: typeof(int),
                oldNullable: true);

            migrationBuilder.AddColumn<int>(
                name: "CompanyId",
                table: "PaiBans",
                nullable: true);

            migrationBuilder.CreateTable(
                name: "DepartmentGroupDevices",
                columns: table => new
                {
                    Id = table.Column<int>(nullable: false)
                        .Annotation("MySql:ValueGenerationStrategy", MySqlValueGenerationStrategy.IdentityColumn),
                    CreateDateTime = table.Column<DateTime>(nullable: false),
                    IsDeleted = table.Column<bool>(nullable: false),
                    DepartmentGroupId = table.Column<int>(nullable: false),
                    HardWareDeviceId = table.Column<int>(nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_DepartmentGroupDevices", x => x.Id);
                    table.ForeignKey(
                        name: "FK_DepartmentGroupDevices_DepartmentGroups_DepartmentGroupId",
                        column: x => x.DepartmentGroupId,
                        principalTable: "DepartmentGroups",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                    table.ForeignKey(
                        name: "FK_DepartmentGroupDevices_HardWareDevices_HardWareDeviceId",
                        column: x => x.HardWareDeviceId,
                        principalTable: "HardWareDevices",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.CreateTable(
                name: "DepartmentGroupWorkers",
                columns: table => new
                {
                    Id = table.Column<int>(nullable: false)
                        .Annotation("MySql:ValueGenerationStrategy", MySqlValueGenerationStrategy.IdentityColumn),
                    CreateDateTime = table.Column<DateTime>(nullable: false),
                    IsDeleted = table.Column<bool>(nullable: false),
                    DepartmentGroupId = table.Column<int>(nullable: false),
                    WorkerId = table.Column<int>(nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_DepartmentGroupWorkers", x => x.Id);
                    table.ForeignKey(
                        name: "FK_DepartmentGroupWorkers_DepartmentGroups_DepartmentGroupId",
                        column: x => x.DepartmentGroupId,
                        principalTable: "DepartmentGroups",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                    table.ForeignKey(
                        name: "FK_DepartmentGroupWorkers_Workers_WorkerId",
                        column: x => x.WorkerId,
                        principalTable: "Workers",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.CreateIndex(
                name: "IX_PaiBans_CompanyId",
                table: "PaiBans",
                column: "CompanyId");

            migrationBuilder.CreateIndex(
                name: "IX_DepartmentGroupDevices_DepartmentGroupId",
                table: "DepartmentGroupDevices",
                column: "DepartmentGroupId");

            migrationBuilder.CreateIndex(
                name: "IX_DepartmentGroupDevices_HardWareDeviceId",
                table: "DepartmentGroupDevices",
                column: "HardWareDeviceId");

            migrationBuilder.CreateIndex(
                name: "IX_DepartmentGroupWorkers_DepartmentGroupId",
                table: "DepartmentGroupWorkers",
                column: "DepartmentGroupId");

            migrationBuilder.CreateIndex(
                name: "IX_DepartmentGroupWorkers_WorkerId",
                table: "DepartmentGroupWorkers",
                column: "WorkerId");

            migrationBuilder.AddForeignKey(
                name: "FK_PaiBans_BanCis_BanCiId",
                table: "PaiBans",
                column: "BanCiId",
                principalTable: "BanCis",
                principalColumn: "Id",
                onDelete: ReferentialAction.Cascade);

            migrationBuilder.AddForeignKey(
                name: "FK_PaiBans_Companies_CompanyId",
                table: "PaiBans",
                column: "CompanyId",
                principalTable: "Companies",
                principalColumn: "Id",
                onDelete: ReferentialAction.Restrict);
        }

        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropForeignKey(
                name: "FK_PaiBans_BanCis_BanCiId",
                table: "PaiBans");

            migrationBuilder.DropForeignKey(
                name: "FK_PaiBans_Companies_CompanyId",
                table: "PaiBans");

            migrationBuilder.DropTable(
                name: "DepartmentGroupDevices");

            migrationBuilder.DropTable(
                name: "DepartmentGroupWorkers");

            migrationBuilder.DropIndex(
                name: "IX_PaiBans_CompanyId",
                table: "PaiBans");

            migrationBuilder.DropColumn(
                name: "CompanyId",
                table: "PaiBans");

            migrationBuilder.AlterColumn<int>(
                name: "BanCiId",
                table: "PaiBans",
                nullable: true,
                oldClrType: typeof(int));

            migrationBuilder.AddColumn<string>(
                name: "Devices",
                table: "DepartmentGroups",
                nullable: true);

            migrationBuilder.AddColumn<string>(
                name: "Workers",
                table: "DepartmentGroups",
                nullable: true);

            migrationBuilder.AddForeignKey(
                name: "FK_PaiBans_BanCis_BanCiId",
                table: "PaiBans",
                column: "BanCiId",
                principalTable: "BanCis",
                principalColumn: "Id",
                onDelete: ReferentialAction.Restrict);
        }
    }
}
